package com.sczmgk.hotline_pzh.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.sczmgk.hotline_pzh.manager.TalkManager;
import com.sczmgk.hotline_pzh.utils.MyLog;

import static android.content.ContentValues.TAG;

public class DBHelper extends SQLiteOpenHelper {

	/**
	 * 数据版本号
	 */
	private static final int DATABASE_VERSION = 2;
	private static final String DATABASE_NAME = "chatmsg.db";
	public static final String TABLE_ID = "_id";

	/** 同步锁对象 */
	public static final Object lockedObject = new Object();
	/** 数据库核心对象 */
	private static DBHelper dbHelper = null;

	private DBHelper(Context context) {
		
		super(context,  DATABASE_NAME, null,DATABASE_VERSION);
		
		MyLog.d(TAG, "当前使用数据库=" + getLoginUserName() + "_" + DATABASE_NAME);
	}

	/**
	 * 聊天记录表
	 */
	public static final String TABLE_CHAT_MSG = "table_chat_msg";
	public static final String DURATION = "duration";
	public static final String FILENAME = "filename";
	public static final String MSGFROM = "msgfrom";
	public static final String GROUPNAME = "groupname";
	public static final String HEIGHT = "height";
	public static final String ISACKED = "isacked";
	public static final String ISDELIVERED = "isdelivered";
	public static final String ISLISTENED = "islistened";
	public static final String LATITUDE = "latitude";
	public static final String LONGITUDE = "longitude";
	public static final String MSGDIR = "msgdir";
	public static final String MSGID = "msgid";
	public static final String MSGTIME = "msgtime";
	public static final String MSGTYPE = "msgtype";
	public static final String STATUS = "status";
	public static final String TEXTMSG = "textmsg";
	public static final String MSGTO = "msgto";
	public static final String TYPENAME = "typename";
	public static final String USERID = "userid";
	public static final String WIDTH = "width";
	/**
	 *智美服务器维一id
	 */
	public static final String CHATID = "chatid";
	public static final String URL = "URL";

	/**
	 *
	 * 未读消息数表
     */

	public static final String TABLE_NEW_MSG_COUNT = "table_new_msg_count";
	/**
	 * 用户表
	 */
	public static final String TABLE_USER = "table_user";
	/**
	 * 用户id
	 */
    public static final String USER_NAME= "user_name";
	/**
	 * 昵称
	 */
	public static final String USER_NICK_NAME ="user_nick_name";
	public static final String USER_SEX = "user_sex";
	public static final String USER_BIRTHDAY = "user_birthday";
	public static final String USER_PHONE = "user_phone";
	public static final String USER_IDCARD = "user_idcard";
	public static final String USER_ADDRESS = "user_address";
	/**
	 * 头像路径
	 */
	public static final String USER_HEADURL = "user_headurl";
	/**
	 * 环信帐号
	 */
	public static final String USER_EASEMOBSACCOUNT = "user_easemobsaccount";
	/**
	 * 环信密码
	 */
	public static final String USER_EASEMOBSPASSWORD = "user_easemobspassword";
	/**
	 * 用户角色
	 */
	public static final String USER_CONSUMERSTYPE = "user_consumerstype";
	/**
	 * 头像本地路径
	 */
	public static final String USER_IMAGELOCALPATH = "user_imagelocalpath";

	/**
	 * 用户邮箱
	 */
	public static final String USER_MAILBOX = "user_mailbox";

	/**
	 * 资讯类型表(indicator头)
	 */
	public static final String INFORMATIN_TITLE_TABLE = "informatin_title_table";
	/**
	 *类型名
	 */
	public static final String INFORMATION_TITLE = "information_title";
	/**
	 * 类型id
	 */
	public static final String INFORMATION_TYPE = "information_type";
	/**
	 * 备注
	 */
	public static final String INFORMATION_REMARK ="information_remark";
	@Override
	public void onCreate(SQLiteDatabase db) {
		//聊天消息表
//		String sql = "CREATE TABLE IF NOT EXISTS "+TABLE_CHAT_MSG+" ("+TABLE_ID+" integer primary key autoincrement,"+DURATION+" integer,"+
//				FILENAME+" varchar(100) DEFAULT NULL,"+MSGFROM+" varchar(100) DEFAULT NULL,"+GROUPNAME+" varchar(100) DEFAULT NULL,"
//				+HEIGHT+" integer,"+ISACKED+" integer,"+ISDELIVERED+" integer,"+ISLISTENED+" integer,"
//				+LATITUDE+" TEXT,"+LONGITUDE+" varchar(100) DEFAULT NULL,"+MSGDIR+" integer,"+MSGID+" varchar(100) DEFAULT NULL,"
//				+MSGTIME+" bigint(100) DEFAULT NULL,"+MSGTYPE+" integer,"+STATUS+" integer,"+TEXTMSG+" TEXT,"
//				+MSGTO+" TEXT,"+TYPENAME+" TEXT,"+URL+" TEXT,"+USERID+" integer,"
//				+WIDTH+" integer,"+MYMSGID+" TEXT UNIQUE)";
        //聊天消息表
		String sql = "CREATE TABLE IF NOT EXISTS "+TABLE_CHAT_MSG+" ("+TABLE_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+DURATION+" TEXT,"+
				FILENAME+" TEXT,"+MSGFROM+" TEXT,"+GROUPNAME+" TEXT,"
				+HEIGHT+" TEXT,"+ISACKED+" TEXT,"+ISDELIVERED+" TEXT,"+ISLISTENED+" TEXT,"
				+LATITUDE+" TEXT,"+LONGITUDE+" TEXT,"+MSGDIR+" TEXT,"+MSGID+" TEXT,"
				+MSGTIME+" TEXT,"+MSGTYPE+" integer,"+STATUS+" TEXT,"+TEXTMSG+" TEXT,"
				+MSGTO+" TEXT,"+TYPENAME+" TEXT,"+URL+" TEXT,"+USERID+" TEXT,"
				+WIDTH+" TEXT,"+CHATID+" TEXT UNIQUE)";
		db.execSQL(sql);
		//新消息数量表
		sql = "CREATE TABLE IF NOT EXISTS "+TABLE_NEW_MSG_COUNT+" ("+TABLE_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+USERID+" TEXT,"
				+MSGDIR+" TEXT,"+MSGID+" TEXT,"+CHATID+" TEXT UNIQUE)";
		db.execSQL(sql);
		//用户表
		sql = "CREATE TABLE IF NOT EXISTS "+TABLE_USER+" ("+TABLE_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+USERID+" TEXT,"
				+USER_NAME+" TEXT,"+USER_NICK_NAME+" TEXT,"+USER_SEX+" TEXT,"+USER_BIRTHDAY+" TEXT,"+USER_PHONE+" TEXT,"
				+USER_IDCARD+" TEXT,"+USER_ADDRESS+" TEXT,"+USER_HEADURL+" TEXT,"+USER_EASEMOBSACCOUNT+" TEXT,"+USER_EASEMOBSPASSWORD+" TEXT,"
				+ USER_MAILBOX + " TEXT,"+USER_CONSUMERSTYPE+" TEXT,"+USER_IMAGELOCALPATH+" TEXT)";
		db.execSQL(sql);

		sql = "CREATE TABLE IF NOT EXISTS "+INFORMATIN_TITLE_TABLE+" ("+TABLE_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+INFORMATION_TITLE+" TEXT,"
				+INFORMATION_TYPE+" TEXT,"+INFORMATION_REMARK+" TEXT)";
		db.execSQL(sql);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		for (int i = oldVersion; i < newVersion; i++) {
			switch (i) {
				case 1:
					version1ToVersion2(db);
					break;
				case 2:
					version2ToVersion3(db);
					break;
			}
		}
	}

	/**
	 * 增加区域表,功能表(存图片路径)
	 * @param db
     */
	private void version1ToVersion2(SQLiteDatabase db){
		String sql = "ALTER TABLE " +TABLE_USER + " ADD COLUMN " + USER_MAILBOX +" TEXT";
		db.execSQL(sql); // 区域表
	}

	/**
	 * 给搜索列表加入用户id
	 * @param db
     */
	private void version2ToVersion3(SQLiteDatabase db){

	}

	/**
	 * 取得登录的用户名
	 * @return
	 */
	private static String getLoginUserName(){
		return TalkManager.getInstance().getLoginUserName();
	}
    /**
     * 关闭资源
     * 
     * @param db
     * @param cursor
     */
    public static void closeAll(SQLiteDatabase db, Cursor cursor) {
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        if (db != null) {
            // db.close();
        }
    }

    /**
     * 销毁dbHelper
     */
    public static void destoryDBHelper() {
        synchronized (lockedObject) {
            if (dbHelper != null) {
                dbHelper.close();
                dbHelper = null;
            }
        }
    }
    /**
     * 获取数据库核心类单例对象
     * 
     * @param context
     * @return
     */
    public static DBHelper getInstance(Context context) {
        synchronized (DBHelper.lockedObject) {
            if (dbHelper == null) {
                dbHelper = new DBHelper(context);
            }
            return dbHelper;
        }
    }
}
